package com.shijie.entity;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.time.LocalDateTime;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User implements Serializable {

    private Long userId;

    private String username;

    private String password;

    private Role role;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;

    public enum Role implements Serializable{

        User("user"),

        Admin("admin");

        private final String value;

        Role(String value) {
            this.value = value;
        }

        // 指定序列化时的方式
        @JsonValue
        public String getValue() {
            return this.value;
        }

        // 指定反序列化时的方式
        @JsonCreator
        public static Role fromValue(String value) {
            for (Role role : Role.values())
                if (role.value.equalsIgnoreCase(value))
                    return role;

            throw new IllegalArgumentException("User.Role 未知参数-没有该参数枚举: " + value);
        }

    }

}
